package com.qs.eggs.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.account.LoginActivity;
import com.qs.eggs.api.HttpApi;
import com.qs.eggs.bean.SortBean;
import com.qs.eggs.refresh.adapter.BaseRecyclerAdapter;

import java.util.HashMap;
import okhttp3.Call;
import okhttp3.Response;


public class SortAdapter extends BaseRecyclerAdapter<SortBean> implements BaseRecyclerAdapter.OnLoadingHeaderCallBack {

    public SortAdapter(Context context, int mode) {
        super(context, mode);

        setOnLoadingHeaderCallBack(this);
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderHolder(ViewGroup parent) {
        return new HeaderViewHolder(mHeaderView);
    }

    @Override
    public void onBindHeaderHolder(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    protected RecyclerView.ViewHolder onCreateDefaultViewHolder(ViewGroup parent, int type) {
        return new SortViewHolder(mInflater.inflate(R.layout.item_sort, parent, false));
    }

    @Override
    protected void onBindDefaultViewHolder(RecyclerView.ViewHolder holder, final SortBean item, int position) {
        final SortViewHolder vh = (SortViewHolder) holder;

        vh.mProName.setText(item.getProname());
        vh.mProPrice.setText("￥"+item.getSaleprice());
        vh.mAvatar.setImageURI(Constants.URL_BASE+"productimages/"+item.getAutoname());

        vh.mAddCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HttpApi.cheakIsLogin(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        if (s.equals("\"true\"")){
                            final HashMap<String,String> map=new HashMap<String, String>();
                            map.put("proid",item.getId());
                            map.put("proname",item.getProname());
                            map.put("price",item.getSaleprice());
                            map.put("count","1");
                            map.put("type",item.getType());
                            map.put("specification","");
                            addCart(map);
                        }else {
                            LoginActivity.show(mContext);
                        }
                    }
                });
            }
        });
    }

    //加入购物车
    private void addCart(HashMap<String,String> map){
        Gson gson=new Gson();
        String str=gson.toJson(map);
        OkGo.post(Constants.URL_BASE+"shoppingcart?action=addShoppingCart")
                .params("data",str)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        if (s.equals("\"true\"")){
                            Toast.makeText(mContext,"加入购物车成功",Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(mContext,"加入购物车失败",Toast.LENGTH_SHORT).show();
                        }
                    }
                });

    }

    private static class SortViewHolder extends RecyclerView.ViewHolder {
        TextView mProName,mProPrice;
        ImageView mAddCart;
        SimpleDraweeView mAvatar;


        public SortViewHolder(View itemView) {
            super(itemView);

            mAddCart=(ImageView)itemView.findViewById(R.id.img_add_cart);
            mProName=(TextView) itemView.findViewById(R.id.tv_proname);
            mProPrice=(TextView) itemView.findViewById(R.id.tv_proprice);
            mAvatar=(SimpleDraweeView) itemView.findViewById(R.id.propic);
        }
    }
}
